package filter

import (
	"gitee.com/kristas/booting-go/framework"
	"gitee.com/kristas/booting-go/framework/core/log"
	"gitee.com/kristas/booting-go/framework/core/statement/types"
	"gitee.com/kristas/booting-go/framework/web"
	"net/http"
	"time"
)

func init() {
	framework.Component(new(LogFilter))
}

type LogFilter struct {
	types.Filter `order:"0" pattern:"/*"`
	Log          log.Logger `wire:""`
}

func (f *LogFilter) DoFilter(w http.ResponseWriter, r *http.Request, chain web.FilterChain) error {
	start := time.Now()
	f.Log.Context(r.Context()).Info("%s %s", r.Method, r.RequestURI)
	err := chain.DoFilter(w, r)
	f.Log.Context(r.Context()).Info("finished: %v", time.Since(start))
	return err
}
